package com.meetablog.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.ArrayList;

import net.sf.json.JSONArray;

public class GetData {
	
	private String	retVal;
	private int	retInt;
	
	
	public String get(Object obj){

		if(null==obj){

			retVal = "";

		} else {
			
			retVal = obj.toString();

		}
		
		return retVal;
	}

	public int getInt(Object obj){
		
		if(null==obj){
			
			retInt = 0;
			
		} else {
			
			retInt = Integer.parseInt(obj.toString());
		}
		
		return retInt;
	}
	
    public static String clobToString(Clob clob) throws SQLException, IOException {

		if (clob == null) {
			return "";
		} 
		   
		StringBuffer strOut = new StringBuffer();
		String str = "";
		BufferedReader br = new BufferedReader(clob.getCharacterStream());
		
		while ((str = br.readLine()) != null) {
			strOut.append(str);
		}
			return strOut.toString();
		} 	
	
	public JSONArray makeJsonResult(ArrayList sqlMap) {
		
		System.out.println("sql =============> " + sqlMap);
		System.out.println("jsonArray start");
		
		JSONArray jsonArray = JSONArray.fromObject(sqlMap); 		//ArrayList 속성을 json객체로
		
		System.out.println("jsonArray end");
		
		/*
		
		while(rs.next()){
			
			Map rsMap =  new HashMap();
			
			for( int i = 1 ; i <= columnCount; i++){
				rsMap.put("" +rsmd.getColumnName(i)+"", ""+rs.getObject(j)+"");
				j++;
			}
			list.add(rsMap);
			j = 1;
		}
		*/
		
		//JSONObject jsonObjet = JSONObject.fromObject(sqlMap); //HashMap 속성을 json 객체로

        //JSONArray jsonArray = JSONArray.fromObject(sqlMap); //ArrayList 속성을 json객체로
	        
	
		//System.out.println(jsonArray);
		//System.out.println(jsonObjet);
		System.out.println("jsonArray =============> " + jsonArray + "\n\n\n");
		
		return jsonArray;
	}
}
